package com.rui.study.algorithm.L_深入拆解JVM_intrinsic;

/**
 * @program: study
 * @description:
 * @author: Yaowr
 * @create: 2018-12-21 08:49
 **/
public class Demo {

    public static int bitCount(int i) {
        // HD, Figure 5-2
        i = i - ((i >>> 1) & 0x55555555);
        i = (i & 0x33333333) + ((i >>> 2) & 0x33333333);
        i = (i + (i >>> 4)) & 0x0f0f0f0f;
        i = i + (i >>> 8);
        i = i + (i >>> 16);
        return i & 0x3f;
    }

    public static void main(String[] args) {
        int sum = 0;
        long start = System.currentTimeMillis();
        for (int i = Integer.MIN_VALUE; i < Integer.MAX_VALUE; i++) {
//            sum += bitCount(i);
            sum += Integer.bitCount(i);     // Intrinsic方法，替换为指定的CPU指令
        }
        System.out.println(sum);
        System.out.println("用时:" + (System.currentTimeMillis() - start) + "ms");
    }
}
